<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>filedrophandler.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_events_filedrophandler.js.html">filedrophandler.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Provides a files drag and drop event detector. It works on
<a name="line17"></a> * HTML5 browsers.
<a name="line18"></a> *
<a name="line19"></a> * @see ../demos/filedrophandler.html
<a name="line20"></a> */
<a name="line21"></a>
<a name="line22"></a>goog.provide(&#39;goog.events.FileDropHandler&#39;);
<a name="line23"></a>goog.provide(&#39;goog.events.FileDropHandler.EventType&#39;);
<a name="line24"></a>
<a name="line25"></a>goog.require(&#39;goog.array&#39;);
<a name="line26"></a>goog.require(&#39;goog.debug.Logger&#39;);
<a name="line27"></a>goog.require(&#39;goog.dom&#39;);
<a name="line28"></a>goog.require(&#39;goog.events&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.BrowserEvent&#39;);
<a name="line30"></a>goog.require(&#39;goog.events.EventHandler&#39;);
<a name="line31"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line32"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line33"></a>
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>/**
<a name="line37"></a> * A files drag and drop event detector. Gets an {@code element} as parameter
<a name="line38"></a> * and fires {@code goog.events.FileDropHandler.EventType.DROP} event when files
<a name="line39"></a> * are dropped in the {@code element}.
<a name="line40"></a> *
<a name="line41"></a> * @param {Element|Document} element The element or document to listen on.
<a name="line42"></a> * @param {boolean=} opt_preventDropOutside Whether to prevent a drop on the
<a name="line43"></a> *     area outside the {@code element}. Default false.
<a name="line44"></a> * @constructor
<a name="line45"></a> * @extends {goog.events.EventTarget}
<a name="line46"></a> */
<a name="line47"></a>goog.events.FileDropHandler = function(element, opt_preventDropOutside) {
<a name="line48"></a>  goog.events.EventTarget.call(this);
<a name="line49"></a>
<a name="line50"></a>  /**
<a name="line51"></a>   * Handler for drag/drop events.
<a name="line52"></a>   * @type {!goog.events.EventHandler}
<a name="line53"></a>   * @private
<a name="line54"></a>   */
<a name="line55"></a>  this.eventHandler_ = new goog.events.EventHandler(this);
<a name="line56"></a>
<a name="line57"></a>  var doc = element;
<a name="line58"></a>  if (opt_preventDropOutside) {
<a name="line59"></a>    doc = goog.dom.getOwnerDocument(element);
<a name="line60"></a>  }
<a name="line61"></a>
<a name="line62"></a>  // Add dragenter listener to the owner document of the element.
<a name="line63"></a>  this.eventHandler_.listen(doc,
<a name="line64"></a>                            goog.events.EventType.DRAGENTER,
<a name="line65"></a>                            this.onDocDragEnter_);
<a name="line66"></a>
<a name="line67"></a>  // Add dragover listener to the owner document of the element only if the
<a name="line68"></a>  // document is not the element itself.
<a name="line69"></a>  if (doc != element) {
<a name="line70"></a>    this.eventHandler_.listen(doc,
<a name="line71"></a>                              goog.events.EventType.DRAGOVER,
<a name="line72"></a>                              this.onDocDragOver_);
<a name="line73"></a>  }
<a name="line74"></a>
<a name="line75"></a>  // Add dragover and drop listeners to the element.
<a name="line76"></a>  this.eventHandler_.listen(element,
<a name="line77"></a>                            goog.events.EventType.DRAGOVER,
<a name="line78"></a>                            this.onElemDragOver_);
<a name="line79"></a>  this.eventHandler_.listen(element,
<a name="line80"></a>                            goog.events.EventType.DROP,
<a name="line81"></a>                            this.onElemDrop_);
<a name="line82"></a>};
<a name="line83"></a>goog.inherits(goog.events.FileDropHandler, goog.events.EventTarget);
<a name="line84"></a>
<a name="line85"></a>
<a name="line86"></a>/**
<a name="line87"></a> * Whether the drag event contains files. It is initialized only in the
<a name="line88"></a> * dragenter event. It is used in all the drag events to prevent default actions
<a name="line89"></a> * only if the drag contains files. Preventing default actions is necessary to
<a name="line90"></a> * go from dragenter to dragover and from dragover to drop. However we do not
<a name="line91"></a> * always want to prevent default actions, e.g. when the user drags text or
<a name="line92"></a> * links on a text area we should not prevent the browser default action that
<a name="line93"></a> * inserts the text in the text area. It is also necessary to stop propagation
<a name="line94"></a> * when handling drag events on the element to prevent them from propagating
<a name="line95"></a> * to the document.
<a name="line96"></a> * @private
<a name="line97"></a> * @type {boolean}
<a name="line98"></a> */
<a name="line99"></a>goog.events.FileDropHandler.prototype.dndContainsFiles_ = false;
<a name="line100"></a>
<a name="line101"></a>
<a name="line102"></a>/**
<a name="line103"></a> * A logger, used to help us debug the algorithm.
<a name="line104"></a> * @type {goog.debug.Logger}
<a name="line105"></a> * @private
<a name="line106"></a> */
<a name="line107"></a>goog.events.FileDropHandler.prototype.logger_ =
<a name="line108"></a>    goog.debug.Logger.getLogger(&#39;goog.events.FileDropHandler&#39;);
<a name="line109"></a>
<a name="line110"></a>
<a name="line111"></a>/**
<a name="line112"></a> * The types of events fired by this class.
<a name="line113"></a> * @enum {string}
<a name="line114"></a> */
<a name="line115"></a>goog.events.FileDropHandler.EventType = {
<a name="line116"></a>  DROP: goog.events.EventType.DROP
<a name="line117"></a>};
<a name="line118"></a>
<a name="line119"></a>
<a name="line120"></a>/** @override */
<a name="line121"></a>goog.events.FileDropHandler.prototype.disposeInternal = function() {
<a name="line122"></a>  goog.events.FileDropHandler.superClass_.disposeInternal.call(this);
<a name="line123"></a>  this.eventHandler_.dispose();
<a name="line124"></a>};
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/**
<a name="line128"></a> * Dispatches the DROP event.
<a name="line129"></a> * @param {goog.events.BrowserEvent} e The underlying browser event.
<a name="line130"></a> * @private
<a name="line131"></a> */
<a name="line132"></a>goog.events.FileDropHandler.prototype.dispatch_ = function(e) {
<a name="line133"></a>  this.logger_.fine(&#39;Firing DROP event...&#39;);
<a name="line134"></a>  var event = new goog.events.BrowserEvent(e.getBrowserEvent());
<a name="line135"></a>  event.type = goog.events.FileDropHandler.EventType.DROP;
<a name="line136"></a>  this.dispatchEvent(event);
<a name="line137"></a>};
<a name="line138"></a>
<a name="line139"></a>
<a name="line140"></a>/**
<a name="line141"></a> * Handles dragenter on the document.
<a name="line142"></a> * @param {goog.events.BrowserEvent} e The dragenter event.
<a name="line143"></a> * @private
<a name="line144"></a> */
<a name="line145"></a>goog.events.FileDropHandler.prototype.onDocDragEnter_ = function(e) {
<a name="line146"></a>  this.logger_.finer(&#39;&quot;&#39; + e.target.id + &#39;&quot; (&#39; + e.target + &#39;) dispatched: &#39; +
<a name="line147"></a>                     e.type);
<a name="line148"></a>  var dt = e.getBrowserEvent().dataTransfer;
<a name="line149"></a>  // Check whether the drag event contains files.
<a name="line150"></a>  this.dndContainsFiles_ = !!(dt &amp;&amp;
<a name="line151"></a>      ((dt.types &amp;&amp;
<a name="line152"></a>          (goog.array.contains(dt.types, &#39;Files&#39;) ||
<a name="line153"></a>          goog.array.contains(dt.types, &#39;public.file-url&#39;))) ||
<a name="line154"></a>      (dt.files &amp;&amp; dt.files.length &gt; 0)));
<a name="line155"></a>  // If it does
<a name="line156"></a>  if (this.dndContainsFiles_) {
<a name="line157"></a>    // Prevent default actions.
<a name="line158"></a>    e.preventDefault();
<a name="line159"></a>  }
<a name="line160"></a>  this.logger_.finer(&#39;dndContainsFiles_: &#39; + this.dndContainsFiles_);
<a name="line161"></a>};
<a name="line162"></a>
<a name="line163"></a>
<a name="line164"></a>/**
<a name="line165"></a> * Handles dragging something over the document.
<a name="line166"></a> * @param {goog.events.BrowserEvent} e The dragover event.
<a name="line167"></a> * @private
<a name="line168"></a> */
<a name="line169"></a>goog.events.FileDropHandler.prototype.onDocDragOver_ = function(e) {
<a name="line170"></a>  this.logger_.finest(&#39;&quot;&#39; + e.target.id + &#39;&quot; (&#39; + e.target + &#39;) dispatched: &#39; +
<a name="line171"></a>                      e.type);
<a name="line172"></a>  if (this.dndContainsFiles_) {
<a name="line173"></a>    // Prevent default actions.
<a name="line174"></a>    e.preventDefault();
<a name="line175"></a>    // Disable the drop on the document outside the drop zone.
<a name="line176"></a>    var dt = e.getBrowserEvent().dataTransfer;
<a name="line177"></a>    dt.dropEffect = &#39;none&#39;;
<a name="line178"></a>  }
<a name="line179"></a>};
<a name="line180"></a>
<a name="line181"></a>
<a name="line182"></a>/**
<a name="line183"></a> * Handles dragging something over the element (drop zone).
<a name="line184"></a> * @param {goog.events.BrowserEvent} e The dragover event.
<a name="line185"></a> * @private
<a name="line186"></a> */
<a name="line187"></a>goog.events.FileDropHandler.prototype.onElemDragOver_ = function(e) {
<a name="line188"></a>  this.logger_.finest(&#39;&quot;&#39; + e.target.id + &#39;&quot; (&#39; + e.target + &#39;) dispatched: &#39; +
<a name="line189"></a>                      e.type);
<a name="line190"></a>  if (this.dndContainsFiles_) {
<a name="line191"></a>    // Prevent default actions and stop the event from propagating further to
<a name="line192"></a>    // the document. Both lines are needed! (See comment above).
<a name="line193"></a>    e.preventDefault();
<a name="line194"></a>    e.stopPropagation();
<a name="line195"></a>    // Allow the drop on the drop zone.
<a name="line196"></a>    var dt = e.getBrowserEvent().dataTransfer;
<a name="line197"></a>    dt.effectAllowed = &#39;all&#39;;
<a name="line198"></a>    dt.dropEffect = &#39;copy&#39;;
<a name="line199"></a>  }
<a name="line200"></a>};
<a name="line201"></a>
<a name="line202"></a>
<a name="line203"></a>/**
<a name="line204"></a> * Handles dropping something onto the element (drop zone).
<a name="line205"></a> * @param {goog.events.BrowserEvent} e The drop event.
<a name="line206"></a> * @private
<a name="line207"></a> */
<a name="line208"></a>goog.events.FileDropHandler.prototype.onElemDrop_ = function(e) {
<a name="line209"></a>  this.logger_.finer(&#39;&quot;&#39; + e.target.id + &#39;&quot; (&#39; + e.target + &#39;) dispatched: &#39; +
<a name="line210"></a>                     e.type);
<a name="line211"></a>  // If the drag and drop event contains files.
<a name="line212"></a>  if (this.dndContainsFiles_) {
<a name="line213"></a>    // Prevent default actions and stop the event from propagating further to
<a name="line214"></a>    // the document. Both lines are needed! (See comment above).
<a name="line215"></a>    e.preventDefault();
<a name="line216"></a>    e.stopPropagation();
<a name="line217"></a>    // Dispatch DROP event.
<a name="line218"></a>    this.dispatch_(e);
<a name="line219"></a>  }
<a name="line220"></a>};
</pre>


</body>
</html>
